﻿namespace MXERP.Blazor.Components
{
    public class CellContext<T>
    {
        private readonly HashSet<T> _selection;
        /// <summary>
        /// The behaviors which can be performed in the cell.
        /// </summary>
        public CellActions Actions { get; }

        public T Item { get; set; }


        public bool Selected
        {
            get
            {
                return _selection.Contains(Item);
            }
        }

        /// <summary>
        /// Creates a new instance.
        /// </summary>
        /// <param name="dataGrid">The data grid which owns this context.</param>
        /// <param name="item">The item displayed in the cell.</param>
        public CellContext(TableRow<T> row)
        {
            _selection = row.Table.Selection;
            Item = row.Item;
            Actions = new CellActions
            {
                SetSelectedItem = x => row.Table.SetSelectedItem(x, row.Item),
            };
        }

        /// <summary>
        /// Represents behaviors which can be performed on a cell.
        /// </summary>
        public class CellActions
        {
            /// <summary>
            /// The function which selects the cell.
            /// </summary>
            public Func<bool, Task> SetSelectedItem { get; init; } = null!;
        }
    }
}
